<html>
<head><meta charset="utf-8"><title>MIR lowering struct update syntax · t-compiler/wg-rfc-2229 · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/index.html">t-compiler/wg-rfc-2229</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/MIR.20lowering.20struct.20update.20syntax.html">MIR lowering struct update syntax</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="218797496"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/MIR%20lowering%20struct%20update%20syntax/near/218797496" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/MIR.20lowering.20struct.20update.20syntax.html#218797496">(Dec 04 2020 at 06:12)</a>:</h4>
<p>So another form pattern takes is struct update pattern </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">X</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">a</span><span class="w"> </span>: <span class="nb">String</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">b</span><span class="w"> </span>: <span class="nb">String</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">X</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">from_other_X</span><span class="p">(</span><span class="n">o</span>: <span class="kp">&amp;</span><span class="nc">Self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">computed</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">format</span><span class="o">!</span><span class="p">(</span><span class="s">"{}a"</span><span class="p">,</span><span class="w"> </span><span class="n">o</span><span class="p">.</span><span class="n">a</span><span class="p">);</span><span class="w"></span>

<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="c1">//  #[rustc_capture_analysis]</span>
<span class="w">            </span><span class="o">||</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">X</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">a</span>: <span class="nc">computed</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="o">..*</span><span class="n">o</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">};</span><span class="w"></span>
<span class="w">        </span><span class="n">c</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">a</span>: <span class="s">""</span><span class="p">.</span><span class="n">into</span><span class="p">(),</span><span class="w"> </span><span class="n">b</span>: <span class="s">""</span><span class="p">.</span><span class="n">into</span><span class="p">()</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X</span>::<span class="n">from_other_X</span><span class="p">(</span><span class="o">&amp;</span><span class="n">x</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">.</span><span class="n">a</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="218797528"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/MIR%20lowering%20struct%20update%20syntax/near/218797528" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/MIR.20lowering.20struct.20update.20syntax.html#218797528">(Dec 04 2020 at 06:13)</a>:</h4>
<p>what is captured here is <code>x[Deref, (1, 0)]</code>, but what MIR expects is <code>x[Deref]</code></p>



<a name="218800241"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/MIR%20lowering%20struct%20update%20syntax/near/218800241" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/MIR.20lowering.20struct.20update.20syntax.html#218800241">(Dec 04 2020 at 07:17)</a>:</h4>
<p>So this is dealt with here: <a href="https://github.com/rust-lang/rust/blob/40dfe1eddd35d65348c40abe12dc5c659d068e2c/compiler/rustc_typeck/src/expr_use_visitor.rs#L419-L426">https://github.com/rust-lang/rust/blob/40dfe1eddd35d65348c40abe12dc5c659d068e2c/compiler/rustc_typeck/src/expr_use_visitor.rs#L419-L426</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>